<!-- 字典数据编辑弹窗 -->
<template>
  <ele-modal
    form
    :width="460"
    :model-value="modelValue"
    :title="isUpdate ? '编辑字典数据' : '新增字典数据'"
    @update:model-value="updateModelValue"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="94px">
      <el-form-item label="字典数据名" prop="name">
        <el-input
          v-model="form.name"
          clearable
          :maxlength="20"
          placeholder="请输入字典数据名"
        />
      </el-form-item>
      <el-form-item label="字典数据值" prop="code">
        <el-input
          v-model="form.code"
          clearable
          :maxlength="20"
          placeholder="请输入字典数据值"
        />
      </el-form-item>
      <el-form-item label="数据类型" prop="type">
        <el-radio-group
          v-model="form.type"
          placeholder="选择数据类型"
        >
          <el-radio
            v-for="item in typeList"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序号" prop="sort">
        <el-input-number
          v-model="form.sort"
          :min="0"
          :max="9999"
          placeholder="请输入排序号"
          controls-position="right"
          class="ele-fluid"
        />
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="form.remark"
          :rows="4"
          type="textarea"
          :maxlength="200"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="updateModelValue(false)">
        取消
      </el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import { reactive, ref, watch } from 'vue'
import {
  updateDictData,
} from '@/api/system/dict-data'
import { message, useFormData } from '@/utils/index'

const props = defineProps({
  /** 弹窗是否打开 */
  modelValue: Boolean,
  /** 修改回显的数据 */
  data: Object,
  /** 字典id */
  id: Number,
})

const emit = defineEmits(['done', 'update:modelValue'])

/** 是否是修改 */
const isUpdate = ref(false)

/** 提交状态 */
const loading = ref(false)

/** 表单实例 */
const formRef = ref(null)

const typeList = ref([
  { label: '字符串', value: 0 },
  { label: '数值', value: 1 },
  { label: '布尔值', value: 2 },
])

/** 表单数据 */
const { form, resetFields, assignFields } = useFormData({
  id: undefined,
  name: '',
  code: '',
  sort: 1000,
  type: 0,
  remark: '',
})

/** 表单验证规则 */
const rules = reactive({
  name: [
    {
      required: true,
      message: '请输入字典数据名',
      type: 'string',
      trigger: 'blur',
    },
  ],
  code: [
    {
      required: true,
      message: '请输入字典数据值',
      type: 'string',
      trigger: 'blur',
    },
  ],
  sort: [
    {
      required: true,
      message: '请输入排序号',
      type: 'number',
      trigger: 'blur',
    },
  ],
})

/** 保存编辑 */
function save() {
  formRef.value?.validate?.((valid) => {
    if (!valid)
      return

    loading.value = true

    updateDictData({
      ...form,
      id: props.id,
    })
      .then((res) => {
        message(res.msg, res.success)
        updateModelValue(false)
        emit('done')
      })
      .finally(() => {
        loading.value = false
      })
  })
}

/** 更新modelValue */
function updateModelValue(value) {
  emit('update:modelValue', value)
}

watch(
  () => props.modelValue,
  (modelValue) => {
    if (modelValue) {
      if (props.data) {
        assignFields(props.data)
        isUpdate.value = true
      }
      else {
        isUpdate.value = false
      }
    }
    else {
      resetFields()
      formRef.value?.clearValidate?.()
    }
  },
)
</script>
